/*
 * 上海远境文化传媒有限公司 版权所有
 */
package facade.base;

import facade.AppContext;
import facade.open.request.base.RoleRequest;
import models.base.AdminRoleOperate;
import models.base.Corp;
import models.base.AdminUser;
import models.base.Role;
import models.constants.DeletedStatus;
import models.enums.UserRole;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import play.i18n.Messages;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import static helper.BeanHelper.safeSetValue;


/**
 * 角色Facade.
 */
public class RoleFacade {

    //新建
    public static Role create(RoleRequest roleRequest){
        AdminUser loginUser = AppContext.currentUser(); //获取登录用户
        Corp corp = AppContext.currentCorp(); // 角色所属公司
        Role role = new Role();
        safeSetValue(roleRequest.name, value -> role.name = value );
        safeSetValue(roleRequest.showOrder, value -> role.showOrder = value );
        safeSetValue(roleRequest.remark, value -> role.remark = value );

        role.deleted = DeletedStatus.UN_DELETED ;
        role.createdAt = new Date() ;
        role.createUser = loginUser ;
        role.corp = corp ;
        role.save();
        AdminRoleOperate.create(role,roleRequest.businessList);
        return role ;
    }

    //修改
    public  static Role update(Role role , RoleRequest roleRequest){
        AdminUser loginUser = AppContext.currentUser(); //获取登录用户

        safeSetValue(roleRequest.name, value -> role.name = value );
        safeSetValue(roleRequest.showOrder, value -> role.showOrder = value );
        safeSetValue(roleRequest.remark, value -> role.remark = value );
        role.modifyAt = new Date();
        role.modifyUser = loginUser;
        role.save() ;
        AdminRoleOperate.create(role,roleRequest.businessList);
        return role ;

    }

    /***************************以上为新建************************************/


    public static List<Role> allCorpRoles(Corp corp) {
        List<Role> roles = new ArrayList<>();
        Set<String> disableRoles = corp.getDisabledRoleSet();
        for (UserRole userRole : UserRole.values()) {
            if (!disableRoles.contains(userRole.name())) {
                Role role = getRoleFromUserRole(userRole);
                roles.add(role);
            }
        }

        if (corp != null) {
            List<Role> ownRoles = Role.findByCorp(corp);
            if (ownRoles.size() > 0) {
                roles.addAll(ownRoles);
            }
        }

        return roles;
    }

    private static Role getRoleFromUserRole(UserRole userRole) {
        Role role = new Role();
        role.id = userRole.name();
        role.name = Messages.get("roles." + userRole.name());
        role.remark = Messages.get("roles." + userRole.name() + ".hint");
        return role;
    }

    /**
     * 按ID返回Role对象.
     */
    public static Role findByCorpAndId(Corp corp, String id) {
        Role role = Role.findByCorpAndId(corp, id);
        if (role == null) {
            UserRole userRole = EnumUtils.getEnum(UserRole.class, StringUtils.upperCase(id));
            if (userRole != null) {
                role = getRoleFromUserRole(userRole);
            }
        }
        return role;
    }
}
